package com.xbn.netty.nio;

import java.nio.ByteBuffer;

/**
 * @author XBN
 * @date 2020/10/13 22:47
 * @email 78742541@qq.com
 */
public class ReadOnlyBuffer {
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(64);
        for (int i = 0; i < 64; i++) {
            byteBuffer.put((byte)i);
        }
        //读取
        byteBuffer.flip();
        //得到一个只读的buffer,该buffer无法再写入数据了
        ByteBuffer readOnlyBuffer = byteBuffer.asReadOnlyBuffer();
        //读取
        while (readOnlyBuffer.hasRemaining())
        {
            System.out.print(readOnlyBuffer.get()+" ");
        }
        //我们不能往里面放数据，如果放数据会出现异常
        readOnlyBuffer.put((byte) 100);
    }
}
